package com.online.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.online.pojo.Course;
import com.online.service.CourseService;
import com.online.mapper.CourseMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import java.util.List;

/**
* @author HDDN
* @description 针对表【on_course】的数据库操作Service实现
* @createDate 2025-02-03 11:01:52
*/
@Service
public class CourseServiceImpl implements CourseService{
    @Autowired
    private CourseMapper iCourseMapper;

    @Override
    public List<Course> selectAll() {
        LambdaQueryWrapper <Course> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Course::getIsDelete,0);
        return iCourseMapper.selectList(lqw);
    }


    @Override
    public List<Course> selectList(Course iCourse) {
        LambdaQueryWrapper <Course> lqw = this.lqw(iCourse);
        return iCourseMapper.selectList(lqw);
    }

    @Override
    public Integer insert(Course iCourse) {
        return iCourseMapper.insert(iCourse);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                Course iCourse = new Course();
                iCourse.setCourseId(Long.parseLong(id));
                iCourse.setIsDelete(1);
                iCourseMapper.updateById(iCourse);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(Course iCourse) {
        return iCourseMapper.updateById(iCourse);
    }

    @Override
    public Page<Course> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Course>)this.selectAll();
    }

    @Override
    public Page<Course> pageList(Course iCourse, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Course>)this.selectList(iCourse);
    }

    public LambdaQueryWrapper lqw(Course iCourse){
        LambdaQueryWrapper <Course> lqw = new LambdaQueryWrapper<>();
        if (iCourse.getCourseId() != null){
            lqw.eq(Course::getCourseId,iCourse.getCourseId());
        }
        if (iCourse.getCourseName() != null){
            lqw.eq(Course::getCourseName,iCourse.getCourseName());
        }
        if (iCourse.getStatus() != null){
            lqw.eq(Course::getStatus,iCourse.getStatus());
        }
        if (iCourse.getAudit() != null){
            lqw.eq(Course::getAudit,iCourse.getAudit());
        }
        if (iCourse.getClassifyId() != null){
            lqw.eq(Course::getClassifyId,iCourse.getClassifyId());
        }
        if (iCourse.getPrice() != null){
            lqw.eq(Course::getPrice,iCourse.getPrice());
        }
        if (iCourse.getUrl() != null){
            lqw.eq(Course::getUrl,iCourse.getUrl());
        }
        if (iCourse.getDescription() != null){
            lqw.eq(Course::getDescription,iCourse.getDescription());
        }
        if (iCourse.getIsDelete() != null){
            lqw.eq(Course::getIsDelete,iCourse.getIsDelete());
        }
        if (iCourse.getUploadId() != null){
            lqw.eq(Course::getUploadId,iCourse.getUploadId());
        }
        if (iCourse.getCreateTime() != null){
            lqw.eq(Course::getCreateTime,iCourse.getCreateTime());
        }
        if (iCourse.getUpdateTime() != null){
            lqw.eq(Course::getUpdateTime,iCourse.getUpdateTime());
        }
        return lqw;
    }
}




